import { BaseMod } from "../../base/BaseMod";
import { errorMgr } from "../../com/ErrorMgr";
import { gameMain } from "../../../GameMain";
import { ImageAsset, instantiate, Layers, Node, resources, Sprite, SpriteFrame, UITransform } from "cc";
import { layerMgr, ViewLayer } from "../../com/LayerMgr";
import { MsgId } from "../../cfg/Msg";
import { Player } from "../player/Player";
import { Views } from "../../cfg/XlsxDef";
import { Xlsx } from "../../cfg/Xlsx";

/**
 * 地图模块
 */
export const mapMod = _ as MapMod;
export class MapMod extends BaseMod {

    curMap: number;
    players: Player[] = [];
    playerDic: { [pid: number]: Player } = {};

    private _mapLayerDic: { [layer: number]: Node } = {};


    onInit(): void {
        (mapMod as any) = this;

        //初始化层级
        let layers = gameMain.mapLayers;
        for (let layer in MapLayer) {
            if (!isNaN(Number(layer))) continue;
            let node = new Node("Layer-" + layer);
            let trans = node.addComponent(UITransform);
            let mapTrans = layers.getComponent(UITransform);
            node.layer = layers.layer;
            trans.setContentSize(mapTrans.contentSize);
            trans.setAnchorPoint(mapTrans.anchorPoint);
            node.setPosition(0, 0);
            layers.addChild(node);
            this._mapLayerDic[MapLayer[layer]] = node;
        }

        this.onMsg(MsgId.addPlayer, msg => {
            for (let p of msg.players) {
                let pNode = instantiate(gameMain.playerPrefab);
                let player = pNode.getComponent(Player);
                player.onCreate(p);
                this.players.push(player);
                this.playerDic[p.pid] = player;
                this.getLayer(MapLayer.Entity).addChild(player.node);
            }
        });

        this.onMsg(MsgId.delPlayer, msg => {
            for (let pid of msg.pids) {
                let player = this.playerDic[pid];
                if (player) {
                    player.node.destroy();
                    this.players.splice(this.players.indexOf(player), 1);
                    delete this.playerDic[pid];
                }
            }
        });

        this.onMsg(MsgId.syncPlayer, msg => {
            for (let p of msg.players) {
                let player = this.playerDic[p.pid];
                if (!player) continue;
                if (p.ang !== undefined) {
                    player.toAng = p.ang - 90;
                }
                if (p.x !== undefined) {
                    player.toVec3.set(p.x, p.y);
                }
            }
        });
        
        layerMgr.open(Views.MainView, _, ViewLayer.Main);
    }

    /**
     * 获取层级节点
     * @param layer 
     * @returns 
     */
    getLayer(layer: MapLayer) {
        return this._mapLayerDic[layer];
    }

    enterMap(mapId: number) {
        this.curMap = mapId;
        let cfg = Xlsx.map.get(this.curMap);
        let node = new Node("bgPic");
        let sprite = node.addComponent(Sprite);
        let trans = node.getComponent(UITransform);
        let mapLayer = this.getLayer(MapLayer.Background);
        let layerTrans = mapLayer.getComponent(UITransform);
        trans.setContentSize(cfg.size[0], cfg.size[1]);
        trans.setAnchorPoint(layerTrans.anchorPoint);
        node.setPosition(0, 0);
        node.layer = mapLayer.layer;
        mapLayer.addChild(node);
        resources.load("art/map/" + cfg.bgPic, ImageAsset, (err, data) => {
            if (err) return errorMgr.error(err);
            sprite.spriteFrame = SpriteFrame.createWithImage(data);
        });
    }

}

/**
 * 地图层级
 */
export enum MapLayer {
    /** 背景层 */
    Background,
    /** 实体层 */
    Entity
}